<?php
/*
 * 调查问卷类
 * 
 */
namespace Home\Logic;

class QuestionnaireLogic{

	public function __construct() {
        $this->q = D('question_answer');
        $this->s = D('suggestion');
        $this->a = D('answer');
    }

    // 获取 用户反馈--问题
    public function getFeedback(){
    	$re = $this->q->where('id=1')->find();
    	return $re;
    }

    // 写入 用户反馈--意见
    public function insertUser_suggestion($data){
    	return $this->s->add($data);
    }	

    //调查问卷--问题
    public function questions(){
        $re = $this->q->where('id != 1 and status=1')->select();
        return $re;
    }
    // 写入 调查问卷--答案
    public function insertUser_questions($data){
        return $this->a->add($data);
    }

    /*获取提交意见的列表带分页*/
    public function getSuggestByPage($page,$limit=20){
        $total_page = ceil($this->countTotal()/$limit);
        $total_page = $total_page>0?$total_page:1;
        $page = $page>0?$page:1;
        $page = $page>$total_page?$total_page:$page;
        $list = $this->s->order('id desc')->limit(($page-1)*$limit,$limit)->select();
        $client_user = new \Home\Logic\UserLogic();
        foreach($list as $k=>$v){
            $user = $client_user->getUserById($v['user_id']);
            $list[$k]['mobile'] = $user['mobile'];
        }
        $_Page  = new \Think\Page($total_page*$limit,$limit);
        $show  = $_Page->show();
        $show = str_replace('/index.php/','/',$show);
        return array('page'=>$show,'list'=>$list);
    }

    /*获取建议总数量*/
    public function countTotal(){
        return $this->s->count();
    }
    
    /*标示建议为已处里*/
    public function dispose($id){
        return $this->s->where('id='.$id)->save(array('status'=>1));//status = 1 已处理的状态
    }

}